<?php ob_start();session_start();?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Quản lý chuyên mục</title>
<link href="../Twitter_Bootstrap/css/bootstrap.css" rel="stylesheet" type="text/css" />
<link href="../CSS/css_admin.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" type="text/css" href="../CSS/validationEngine.jquery.css"/>

<!--<script type="text/javascript" src="../Scripts/jquery-1.7.2.min.js"></script> -->
<script type="text/javascript" src="../Twitter_Bootstrap/js/jquery.js"></script>
<script type="text/javascript" src="../Twitter_Bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript" src="../Scripts/jquery.validationEngine.js"></script>
<script type="text/javascript" src="../Scripts/jquery.validationEngine-vi.js"></script>
<script type="text/javascript" src="../Scripts/TopicManagementJS.js"></script>
<script type="text/javascript" src="../Scripts/jquery.tablednd.0.6.min.js"></script>


<style type="text/css">
<!--
#tbResult td,#tbResult th
{
	text-align:center;
}
-->
</style>
</head>

<body>
 <?php 
	 include("../Connections/connect_admin.php");
	 ?>
<div class="container" id="Container">
<!--Menu top -->
  <div id="menu">
  	<?php
		include("MainMenu.php");
	?>
    </div>
  </div>
  <div id="divContain">
  <!--Content -->
   <div class="divTitle"><span class="titlePage" >Quản lý chuyên mục</span></div>
  	<div style="padding-top:30px" >
     <form action="" method="get" id="formSearch" class="form-horizontal">
    	<select name="" id="cbxTopic_Search">
    	<option value="0">---Chuyên mục cha---</option>
     <?php
	  
		function Menu($cap = 0,$gach = '&gt;&gt;  ', $arr = NULL){  
		if(!$arr) $arr = array();//khoi tao 1 array co ten la arr  
		$sql = mysql_query("SELECT * FROM topic WHERE IsDeleted = 0 and ParentID = ".$cap." order by OrderIndex");  
		while($row = mysql_fetch_assoc($sql)){  
		$arr[] = array('id'=>$row['TopicID'],'ten'=>$gach.$row['TopicName']);  
		$arr = Menu($row['TopicID'],$gach.'&gt;&gt;   ',$arr);  
		}  
		return $arr;  
		}  
		$Menu = Menu(0);  

		 foreach($Menu as $k=>$row)
		 {
		 ?>
		 <option value="<?php echo $row["id"]; ?>"><?php echo $row["ten"]; ?></option>
		<?php
		 }
		?>       	
        </select>
         <a type="button" class="btn btn-primary" id="btnAddTopic" href="#updateTopic" data-toggle="modal"><i class="icon-white icon-plus-sign"></i> Tạo chuyên mục mới</a>
         <a type="button" class="btn btn-primary" id="btnSaveOrder"><i class="icon-white icon-th-list"></i> Lưu thứ tự hiển thị</a>
    </form>
     <?php
	if(isset($_GET["e"]))
	{
		$error=intval($_GET["e"]);
		switch($error)
		{
			case 0:
	?>
    <div class="alert alert-success">
    	    <a class="close" data-dismiss="alert" href="#">&times;</a>
            <strong>Thêm mới chuyên mục THÀNH CÔNG</strong>
    </div>
    <?php
			break;
			case 1:
	?>
     <div class="alert alert-error">
    	    <a class="close" data-dismiss="alert" href="#">&times;</a>
            <strong>Thêm mới chuyên mục THẤT BẠI</strong>
    </div>
     <?php
	 		break;
			case 2:
	?>
     <div class="alert alert-success">
    	    <a class="close" data-dismiss="alert" href="#">&times;</a>
            <strong>Cập nhật chuyên mục THÀNH CÔNG</strong>
    </div>
     <?php
	 		break;
			case 3:
	?>
     <div class="alert alert-error">
    	    <a class="close" data-dismiss="alert" href="#">&times;</a>
            <strong>Cập nhật chuyên mục THẤT BẠI</strong>
    </div>
    <?php
			break;
		}	
		
	}
	?>
    <table class="table table-bordered table-striped  " id="tbResult" width="100%" border="0" cellspacing="0" cellpadding="0" >
 <thead>
  <tr class="nodrop nodrag">
  	<th scope="col">Thứ tự</th>
    <th scope="col" style="display:none"></th>
    <th scope="col">Tên chuyên mục</th>
    <th scope="col">Liên kết</th>
    <th scope="col">Mô tả</th>
    <th scope="col">Cho phép tạo tin </th>
    <th scope="col">&nbsp;</th>
    <th scope="col">&nbsp;</th>
  </tr>
  </thead>
     <tbody>
 
  <?php
  $query="select * from topic where IsDeleted = 0 and ParentID = 0 order by OrderIndex";
  $result=mysql_query($query);
  $j=1;
  while($row=mysql_fetch_assoc($result))
  {
  ?>
   <tr id="<?php echo $j; ?>">
  	<td class="order"><?php echo $row["OrderIndex"]; ?></td>
    <td style="display:none"><?php echo $row["TopicID"]; ?></td>
    <td><?php echo $row["TopicName"]; ?></td>
    <td><?php echo $row["Link"]; ?></td>
    <td><?php echo $row["Description"]; ?></td>
    <td><input type="checkbox" disabled="disabled" <?php if($row["IsAllowWriteNews"]==1) echo 'checked="checked"'; ?>/></td>
    <td><a href="#" title="Xóa" class="delete"><img src="../Images/Icons/delete.png" alt="delete" /></a></td>
    <td><a href="#updateTopic" data-toggle="modal" title="Sửa" class="update"><img src="../Images/Icons/page_edit.png" alt="update" /></a></td>
   
  </tr>
  
  <?php $j++; }?>
</tbody>

    </table>
 <p id="myDebug"></p>
	<script>
	$(document).ready(function(){
									 
							 //Lưu ý: phải đặt id cho từng dòng mới sử dụng onDrop được
							   $("#tbResult").tableDnD({									
										onDrop: function(table, row) 
										{
											var index=0;
											$('#tbResult tbody tr td.order').each(function () {
											$(this).html(index);
											index++;
											});
										}									
									});
	<!--------------------------------------Save order Index--------------------------------------------->	
	$("#btnSaveOrder").click(function(){
									  var orderList='';
									  $('#tbResult tbody tr').each(function(){
											orderList+=$(this).children('td').eq(1).text()+',';
																			});
									  if(orderList!='')
									  {
										  $.ajax({
													url:"process/Topic_edit.php",
													data:{task:"UpdateOrder",orderList:orderList},
													type:"get",
													dataType:"json",
													success:function(res)
														 {
																			
																	 alert(res.msg);
																			 
														}
										 });
									  }
									 // alert(orderList);
									  });
	 <!--------------------------------------Delete topic--------------------------------------------->
	$('#tbResult a.delete').live('click',function(){
									var check=confirm("Bạn thật sự muốn xóa?");
									var $temp=$(this);
									if(check)
									{
										var id=$(this).parent().parent().children().eq(1).text();
										$.ajax({
											url: 'process/Topic_edit.php',
											data: { topicID: id, task: 'delete', deleteduser:'<?php echo $_SESSION["username"]; ?>' },
											dataType: 'json',
											type: 'get',
											error:function()
											{
												alert('error');
											},
											success: function (res) {
												if(res.ok)
												{
													alert(res.msg);
													$temp.parent().parent().remove();
													$('#cbxTopic_Search option[value='+id+'],#cbxTopic option[value='+id+']').remove();
													
												}
												
											}
										});
									}
								
												  });
	
							   });
	
	
	
							
	</script>
	</div>
  <!--footer -->
  </div>
  <div id="footer">
       <?php
     	include("Footer.php");
	 ?>
  </div>
</div>

 

<!---------------------------Popup Thêm/Cập nhật chuyên mục----------------------------->
    <div class="modal hide fade"  id="updateTopic">
    <div class="modal-header">
    <a class="close" data-dismiss="modal">×</a>
    <h3></h3>
    </div>
    <form action="process/Topic_edit.php" method="get" id="formTopic" style="margin-bottom:0px">
    <div class="modal-body">
    <p>
    	<input type="hidden" id="topicID" name="topicID" />
        <input type="hidden" id="task" name="task" />
        
        	<table width="100%" border="0" cellspacing="0" cellpadding="0">
            	<tr>
                	<td>Chuyên mục cha:</td>
                    <td>
                    <select name="Parent" id="cbxTopic" >
                    	<option value="0">---Chuyên mục cha---</option>
						<?php           
                         foreach($Menu as $k=>$row)
                         {
                         ?>
                         <option value="<?php echo $row["id"]; ?>"><?php echo $row["ten"]; ?></option>
                        <?php
                         }
                        ?>       	
                    </select>
                    </td>
                </tr>
                  <tr>
                    <td>Tên chuyên mục:</td>
                    <td><input name="txtName" type="text" id="txtTopicName" class="validate[required]" /></td>
                  </tr>
                  <tr>
                    <td>Liên kết:</td>
                    <td><input name="txtLink" type="text" id="txtLink" /></td>
                  </tr>
                   <tr>
                    <td></td>
                    <td><label class="checkbox"><input type="checkbox" value="1" id="chkIsCreateNews" name="IsCreateNews" checked="checked" /> Cho phép tạo tin trong chuyên mục này</label></td>
                  </tr>
                  <tr>
                    <td>Mô tả:</td>
                    <td><textarea name="txtDescription" id="txtDescription" rows="3"></textarea></td>
                  </tr>
                  
              </table>

       
    </p>
    </div>
    <div class="modal-footer">
    <a href="#" class="btn" data-dismiss="modal">Đóng lại</a>
     <button type="submit" class="btn btn-primary" id="btnSave">Cập nhật</button>
    </div>
     </form>
     <script>
		//-----------------------Validation----------------------------------
			$(document).ready(function(){
							   $("#formTopic").validationEngine();
							   });
		</script>
    </div>

</body>
</html>
